package com.chris.cc.user.model.vo;

import com.chris.cc.common.base.BaseEntity;
import com.chris.cc.user.model.entity.Permission;
import com.chris.cc.user.model.entity.Role;
import com.chris.cc.user.model.entity.User;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * <p>
 * Author: haoka (haokang207@126.com)
 * <p>
 * Date: 2018/8/27 10:55
 **/
@Getter
@Setter
@Builder
public class UserInfoVO extends BaseEntity {
    private Long userId;
    private String tel;
    private String name;
    private String email;
    /**
     * 只在根据手机号获取单个用户时不为空
     */
    private String password;
    private Long deptId;
    private String deptName;
    private Boolean enable;
    private List<Long> roleIds;
    private List<String> roles;
    private List<String> roleNames;
    private List<String> permissions;


    public static UserInfoVO from(User u, String deptName, List<Role> roles, List<Permission> permissions) {
        if (u == null) {
            return null;
        }
        List<Long> rolesIdsStr = new ArrayList<>();
        if (roles != null) {
            rolesIdsStr = roles.stream().map(Role::getRoleId).collect(Collectors.toList());
        }
        List<String> rolesStr = new ArrayList<>();
        if (roles != null) {
            rolesStr = roles.stream().map(Role::getCode).collect(Collectors.toList());
        }
        List<String> roleNamesStr = new ArrayList<>();
        if (roles != null) {
            roleNamesStr = roles.stream().map(Role::getName).collect(Collectors.toList());
        }
        List<String> permissionsStr = new ArrayList<>();
        if (permissions != null) {
            permissionsStr = permissions.stream().map(Permission::getCode).collect(Collectors.toList());
        }
        UserInfoVO vo = UserInfoVO.builder()
                .userId(u.getUserId())
                .deptId(u.getDeptId())
                .tel(u.getTel())
                .email(u.getEmail())
                .password(u.getPassword())
                .enable(u.getEnable())
                .name(u.getName())
                .deptName(deptName)
                .roleIds(rolesIdsStr)
                .roles(rolesStr)
                .roleNames(roleNamesStr)
                .permissions(permissionsStr)
                .build();
        vo.setCreateDate(u.getCreateDate());
        vo.setModifyDate(u.getModifyDate());
        return vo;
    }
}
